/*
 * @Author: TT-hzy
 * @Date: 2024-07-02 18:28:07
 * @LastEditors: TT-hzy 
 * @LastEditTime: 2024-07-05 11:31:25
 * @Description:
 */
import { Context } from "egg";

export default (secret: string) => {
  return async function jwtErr(ctx: Context, next: () => Promise<any>) {
    const token = ctx.service.actionToken.getHeaderToken();
    if (token != 'null' && token) {
      try {
        ctx.app.jwt.verify(token, secret);
        await next();
      } catch (err) {
        console.log(err);
        ctx.configReturnBody({
          isOk: false,
          ctx: ctx,
          msg: 'token已过期，请重新登录'
        });
      };

    } else {

      ctx.configReturnBody({
        isOk: false,
        ctx: ctx,
        msg: 'token不存在'
      })
    }
  };
}